/*
https://leetcode-cn.com/problems/maximum-points-you-can-obtain-from-cards/solution/ke-huo-de-de-zui-da-dian-shu-by-leetcode-7je9/
 */
import java.util.Arrays;

public class Solution1423 {
    public int maxScore(int[] cardPoints, int k) {
        int n=cardPoints.length;
        int sum=0;
        int min=Integer.MAX_VALUE;
        for (int i=0;i<n;i++){
            sum=sum+cardPoints[i];
            if (i==n-k-1){
                min=sum;
            }
            if (i>=n-k){
                sum=sum-cardPoints[i-n+k];
                min=Math.min(min,sum);
            }
        }
        min=Math.min(min,sum);
        int s= Arrays.stream(cardPoints).sum();
        return s-min;
    }

    public static void main(String[] args) {
        System.out.println(new Solution1423().maxScore(new int[]{1,2,3,4,5,6,1},3));
    }
}
